class Solution:
    def isValidSerialization(self, preorder: str) -> bool:
        preorder = preorder.split(",")
        if len(preorder) <= 0:
            return True
        stack = [0]
        tmp = [0] * len(preorder)
        i = 1
        while len(stack) > 0:
            if preorder[stack[-1]] == '#' or tmp[stack[-1]] == 2:
                stack.pop(-1)
                if len(stack) != 0:
                    tmp[stack[-1]] += 1
                continue
            if i >= len(preorder):
                return False
            stack.append(i)
            i += 1
        if i >= len(preorder):
            return True
        else:
            return False


if __name__ == '__main__':
    so = Solution()
    nums = "1"
    r = so.isValidSerialization(nums)
    print(r)
